package com.example.android_app.network.services.consmp;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

import com.example.android_app.GameActivity;
import com.example.android_app.network.Constants;
import com.example.android_app.network.ServiceCall;
import com.pervasive.project.unified.data.model.request.DropCardRequest;

public class DropCardRequestService implements ServiceCall{

	private HttpPut request;
	private DefaultHttpClient httpclient;
	private GameActivity gameActivity;
	private DropCardRequest dropCardRequest;
	
	public DropCardRequestService(DefaultHttpClient httpclient, GameActivity gameActivity, DropCardRequest dropCardRequest) {
		// TODO Auto-generated constructor stub
		this.request = new HttpPut("http://"+Constants.SERVICE_HOST+":"+Constants.SERVICE_PORT+"/services/rest/services/consumeDropCardRequestService");
		//request.addHeader("Accept", "text/xml");
		
		this.httpclient = httpclient;
		this.gameActivity = gameActivity;
		this.dropCardRequest = dropCardRequest;
	}
	
	private static String read(InputStream instream) 
    {
        StringBuilder sb = null;
        try 
        {
             sb = new StringBuilder();
             BufferedReader r = new BufferedReader(new InputStreamReader(instream));
             for (String line = r.readLine(); line != null; line = r.readLine()) 
             {
             	sb.append(line);
             }

			instream.close();

        } catch (IOException e) 
        {
        	
        }
        return sb.toString();

    }
	
	@Override
	public void call() {
		// TODO Auto-generated method stub
		HttpResponse response = null;
		InputStream inputStream = null;
		Serializer serializer = new Persister();
		OutputStream outputStream = new ByteArrayOutputStream();
		
		try {
			serializer.write(dropCardRequest, outputStream);
			request.setEntity(new StringEntity(outputStream.toString()));
			
			response = httpclient.execute(request);
			HttpEntity httpEntity = response.getEntity();
			inputStream = httpEntity.getContent();
			
			String jaxrsmessage = read(inputStream);
			System.out.println(" >>>>> "+jaxrsmessage);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
